from rest_framework import serializers

from task.models import ShopTask, BrowseTask, TaskQuest


class CreateShopTaskSerializer(serializers.ModelSerializer):
    """支付任务序列化器"""

    def validate(self, attrs):
        if attrs['in_page_time1'] >= attrs['in_page_time2']:
            raise serializers.ValidationError('页面停留时间区间填写有误')
        if attrs['store_jump_count1'] > attrs['store_jump_count2']:
            raise serializers.ValidationError('店铺跳转区间填写有误')
        if attrs['start_time1'] >= attrs['start_time2']:
            raise serializers.ValidationError('任务执行时间区间填写有误')
        return attrs

    class Meta:
        model = ShopTask
        # fields = '__all__'
        fields = ('user_id', 'status', 'task_name', 'id',
                  "product_id", "open_mode", "sku_attr", "search_keyword", "is_add_shopping", "is_add_wish",
                  "is_add_store_wish", "freight_name", "search_click_count", "in_page_time1", "in_page_time2",
                  "start_time1", "start_time2", "country", "sku_buy_count", "account_level", "task_num",
                  "store_jump_count1", "store_jump_count2", 'task_time')
        extra_kwargs = {
            "task_time": {"read_only": True},  # 字段: {参数：参数值}
            "id": {"read_only": True}  # 字段: {参数：参数值}
        }


class CreateBrowseTaskSerializer(serializers.ModelSerializer):
    """浏览任务序列化器"""

    def validate(self, attrs):
        if attrs['in_page_time1'] > attrs['in_page_time2']:
            raise serializers.ValidationError('页面停留时间区间填写有误')
        if attrs['store_jump_count1'] > attrs['store_jump_count2']:
            raise serializers.ValidationError('店铺跳转区间填写有误')
        if attrs['start_time1'] > attrs['start_time2']:
            raise serializers.ValidationError('任务执行时间区间填写有误')
        return attrs

    class Meta:
        model = BrowseTask
        # fields = '__all__'
        fields = ('user_id', 'status', 'task_name', 'id',
                  "product_id", "open_mode", "sku_attr", "search_keyword", "is_add_shopping", "is_add_wish",
                  "is_add_store_wish", "search_click_count", "in_page_time1", "in_page_time2", "start_time1",
                  "start_time2", "country", "task_num", "store_jump_count1", "store_jump_count2"
                  )
        extra_kwargs = {
            "task_time": {"read_only": True},  # 字段: {参数：参数值}
            "id": {"read_only": True}  # 字段: {参数：参数值}
        }


class TaskQuestSerializer(serializers.ModelSerializer):
    """分配过的任务序列化器"""

    class Meta:
        model = TaskQuest
        # fields = '__all__'
        fields = ('quest_id', )
        extra_kwargs = {}
